using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.UI.Base;
using Habanero.UI.Win;
using UseCaseGenerator.BO;

namespace UseCaseGenerator.UI
{
    /// <summary>
    /// A parent-child grid control used for adding and editing UseCases and their UseCase_UseCaseItem.
    /// </summary>
    public class UseCasesAndUseCase_UseCaseItemsControl : UserControlWin, IFormControl
    {
        private readonly IControlFactory _controlFactory;
        private readonly IReadOnlyGridControl _useCasesGridControl;

        public UseCasesAndUseCase_UseCaseItemsControl(IControlFactory controlFactory)
        {
            _controlFactory = controlFactory;

            _useCasesGridControl = SetupParentControl();
            IReadOnlyGridControl useCase_UseCaseItemGridControl = SetupChildControl();

            // Add the grids to the control. The UseCases grid fills the top part of the control.  
            // Change the size here depending on how many items you want to see in this grid.  
            // The UseCase_UseCaseItem grid fills the rest of the control.
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            _useCasesGridControl.Height = 200;
            layoutManager.AddControl(_useCasesGridControl, BorderLayoutManager.Position.North, true);
            layoutManager.AddControl(useCase_UseCaseItemGridControl, BorderLayoutManager.Position.Centre);

            // Connect the UseCases and UseCase_UseCaseItem grids so that when an item is selected
            // in the UseCases grid, the correct UseCase_UseCaseItem are shown in the child grid.
            _useCasesGridControl.Grid.BusinessObjectSelected += 
                delegate
                    {
                        UseCase selectedUseCase = _useCasesGridControl.Grid.SelectedBusinessObject as UseCase;
                        if (selectedUseCase == null) return;
                        useCase_UseCaseItemGridControl.SetBusinessObjectCollection(selectedUseCase.UseCases);
                    };
        }

        private IReadOnlyGridControl SetupParentControl()
        {
            // Create the UseCases read-only grid
            IReadOnlyGridControl useCasesGridControl = _controlFactory.CreateReadOnlyGridControl();

            // Used to specify a custom UI def
            useCasesGridControl.Initialise(ClassDef.Get<UseCase>(), "default");

            // Load the collection of UseCases.  You can set the criteria and order by values here.
            BusinessObjectCollection<UseCase> useCasesCollection = new BusinessObjectCollection<UseCase>();
            useCasesGridControl.SetBusinessObjectCollection(useCasesCollection);

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            useCasesGridControl.FilterControl.Visible = false;
            //useCasesGridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //useCasesGridControl.Buttons.ShowDefaultDeleteButton = true;

            return useCasesGridControl;
        }

        private IReadOnlyGridControl SetupChildControl()
        {
            // Create the UseCase_UseCaseItem read-only grid to show the child items
            IReadOnlyGridControl useCase_UseCaseItemGridControl = _controlFactory.CreateReadOnlyGridControl();

            // Used to specify a custom UI def
            useCase_UseCaseItemGridControl.Initialise(ClassDef.Get<UseCaseItem>(), "default");

            // Creates an empty collection of UseCase_UseCaseItem to put in the child grid
            BusinessObjectCollection<UseCaseItem> t2Collection = new BusinessObjectCollection<UseCaseItem>();
            useCase_UseCaseItemGridControl.SetBusinessObjectCollection(t2Collection);

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            useCase_UseCaseItemGridControl.FilterControl.Visible = false;
            //useCase_UseCaseItemGridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //useCase_UseCaseItemGridControl.Buttons.ShowDefaultDeleteButton = true;

            return useCase_UseCaseItemGridControl;
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form) { }

        public IReadOnlyGridControl UseCasesGridControl
        {
            get { return _useCasesGridControl; }
        }
    }
}
